package com.dk.service.serviceImpl;

import com.dk.mapper.OrderMapper;
import com.dk.mapper.OrderRecordMapper;
import com.dk.mapper.TagMapper;
import com.dk.mapper.UserMapper;
import com.dk.pojo.Order;
import com.dk.pojo.OrderRecord;
import com.dk.pojo.User;
import com.dk.service.OrderService;
import com.dk.util.DateUtil;
import com.dk.util.ParseUtil;
import com.dk.vo.order.OrderInfo;
import com.dk.vo.order.OrderReceive;
import com.dk.vo.order.OrderShow;
import com.jack.weChatSecurity.core.WeChatUser;
import com.jack.weChatSecurity.core.SecurityHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderRecordMapper orderRecordMapper;

    private OrderInfo parseOrderToOrderInfo(Order order){
        OrderInfo orderInfo =new OrderInfo();
        orderInfo.setId(order.getId());
        OrderInfo.Class c= orderInfo.getSubject();
        c.setId(order.getClassId());
        c.setName(order.getClassName());
        c.setAddress(order.getClassAddress());
        c.setDay(order.getClassDay());
        String time=order.getClassTime();
        int tb= ParseUtil.stringParseInt(time.split("-")[0]);
        int te= ParseUtil.stringParseInt(time.split("-")[1]);
        c.setTime(new int[]{tb,te});
        orderInfo.getTeacher().setName(order.getTeacherName());
        OrderInfo.User customer= orderInfo.getCustomer();
        User user= userMapper.queryUserById(order.getUid());
        customer.setId(user.getId());
        customer.setAvatarUrl(user.getAvatarUrl());
        customer.setName(user.getName());
        customer.setStuId(user.getStuId());
        customer.setMajor(user.getMajor());
        orderInfo.setPrice(order.getPrice());
        orderInfo.setRemark(order.getRemark());
        orderInfo.setTags(tagMapper.queryTagsByOrderId(order.getId()));
        orderInfo.setState(order.getState());
        if(order.getRid()!=null){
            OrderInfo.User receiver=new OrderInfo.User();
            User u=userMapper.queryUserById(order.getRid());
            receiver.setId(u.getId());
            receiver.setAvatarUrl(u.getAvatarUrl());
            receiver.setName(u.getName());
            receiver.setStuId(u.getStuId());
            receiver.setMajor(u.getMajor());
            orderInfo.setReceiver(receiver);
        }
        return orderInfo;
    }

    private List<OrderShow> parseOrderToOrderShow(List<Order> orderList){
        List<OrderShow> orderShows=new ArrayList<>();
        for(Order order:orderList){
            OrderShow orderShow=new OrderShow();
            orderShow.setId(order.getId());
            orderShow.setClassName(order.getClassName());
            orderShow.setClassTime(order.getClassTime());
            orderShow.setPrice(order.getPrice());
            String uid=order.getUid();
            User user=userMapper.queryUserById(uid);
            orderShow.setAvatarUrl(user.getAvatarUrl());
            orderShow.setNickName(user.getNickName());
            orderShow.setTags(tagMapper.queryTagsByOrderId(order.getId()));
            orderShows.add(orderShow);
        }
        return orderShows;
    }

    public List<OrderShow> getAllOrders(){
        List<Order> list= orderMapper.queryOrders(0,99,0);
        return parseOrderToOrderShow(list);
    }

    public List<OrderShow> getOrdersByUid(String uid, int state){
        List<Order> list=orderMapper.queryOrdersByUser(state,uid);
        return parseOrderToOrderShow(list);
    }

    public OrderInfo getOrder(String orderId){
        Order order=orderMapper.queryOrderById(orderId);
        return parseOrderToOrderInfo(order);
    }

    @Transactional
    public void saveOrder(OrderReceive orderReceive){
        Order order=new Order();
        String id=UUID.randomUUID().toString().replaceAll("-","");
        order.setId(id);
        OrderInfo.Class subject= orderReceive.getSubject();
        order.setClassName(subject.getName());
        order.setClassId(subject.getId());
        order.setClassAddress(subject.getAddress());
        order.setClassDay(subject.getDay());
        int[] time=subject.getTime();
        order.setClassTime(time[0]+"-"+time[1]);
        order.setTeacherName(orderReceive.getTeacher().getName());
        order.setPrice(orderReceive.getPrice());
        order.setRemark(orderReceive.getRemark());
        order.setUid(SecurityHelper.getWeChatUser().getId());
        order.setState(0);
        int[] tags=orderReceive.getTags();
        orderMapper.saveOrder(order);
        for(int i=0;i<(tags.length>2?2:tags.length);i++){
            tagMapper.saveOrderTag(id,tags[i]);
        }
    }

    public void updateOrder(String orderId,double price,String remark){
        orderMapper.updateOrder(orderId,price,remark);
    }

    @Transactional
    public void receiveOrder(String orderId){
        WeChatUser weChatUser = SecurityHelper.getWeChatUser();
        orderMapper.updateOrderRid(orderId,weChatUser.getId());
        orderMapper.updateOrderState(orderId,1);
    }

    public void finishOrder(String orderId){
        orderMapper.updateOrderState(orderId,2);
    }

    @Transactional
    public void checkOrder(String orderId){
        orderMapper.updateOrderState(orderId,3);
        Order order= orderMapper.queryOrderById(orderId);
        OrderRecord orderRecord=new OrderRecord();
        orderRecord.setOid(orderId);
        orderRecord.setUid(order.getUid());
        orderRecord.setFinishTime(DateUtil.getTimestamp());
        orderRecordMapper.addOrderRecord(orderRecord);
    }
}
